<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                round
                @click="toAddRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list" stripe>
              <el-table-column label="当页序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="success"
                    round
                    @click="assignPermission(scope.row.id,scope)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    icon="el-icon-edit"
                    round
                    @click="toPutRoles(scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    type="danger"
                    size="small"
                    round
                    icon="el-icon-delete"
                    @click="toDelRole(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :total="total"
                layout="sizes,total,prev,pager,next,jumper"
                :page-size="pages.pagesize"
                :page-sizes="[2, 4, 6, 8]"
                @current-change="pageChange"
                @size-change="sizeChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  disabled
                  style="width: 400px"
                  :value="formData.name"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  disabled
                  style="width: 400px"
                  :value="formData.companyAddress"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  disabled
                  style="width: 400px"
                  :value="formData.mailbox"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                  :value="formData.remarks"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 角色编辑弹出框 -->
      <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button style="width: 100px" @click="btnCancel">取消</el-button>
            <el-button
              style="width: 100px"
              type="primary"
              @click="btnOK"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹窗 -->
      <el-dialog
        title="权限分配"
        :visible="assignPermShow"
        @close="btnPermCancel"
      >
        <el-tree
          ref="permTree"
          :data="assignForm"
          show-checkbox
          check-strictly
          node-key="id"
          :default-checked-keys="permIdsList"
          :props="{ label: 'name' }"
          highlight-current
          :indent="50"
        />
        <!-- 确定/取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button
              type="primary"
              style="width: 100px"
              @click="btnPermOK"
            >确定</el-button>
            <el-button
              style="width: 100px"
              @click="btnPermCancel"
            >取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getCompanyInfo,
  getRoleList,
  delRoles,
  putRoles,
  getOneRole,
  addRoles,
  getRole
} from '@/api/setting'
import { mapGetters } from 'vuex'
import { transListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permisson'
export default {
  data() {
    return {
      // 弹出分配权限弹框
      roleId: '',
      assignPermShow: false,
      // 绑定分配数据
      assignForm: [],
      // 原有的权限数组
      permIdsList: [],
      // 角色弹出框的数据对象
      roleForm: {
        name: '',
        description: ''
      },
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '', // 邮箱
        remarks: '' // 备注
      },
      pages: {
        page: 1,
        pagesize: 4
      },
      list: [],
      total: 0,
      showDialog: false,
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '字符必须在2-10位之间', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // 获取公司的id
    ...mapGetters(['companyId']),

    title() {
      return this.roleForm.id ? '角色编辑' : '角色新增'
    }
  },
  // 钩子获取数据
  created() {
    this.toGetCompanyInfo()
    this.toGetRoleList()
  },
  methods: {
    // 权限业务开始
    // 取消
    btnPermCancel() {
      // 清空数据, 防止减少权限时,减少的数据不会再出现
      this.permIdsList = []
      this.assignPermShow = false
    },
    // 确定提交
    async btnPermOK() {
      try {
        // getCheckedKeys 是el-tree中的一个属性返回节点中选择的node-key中的值组成的数组
        const data = { id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() }
        // 给角色分配权限
        await getRole(data)
        this.$message.success('提交成功')
        // 刷新页面
        this.assignList(this.roleId)
        this.assignPermShow = false
      } catch (error) {
        console.log(error)
      }
    },
    // 打开分配弹窗
    assignPermission(id) {
      this.roleId = id
      this.assignList()
      this.oneAssognList()
      this.assignPermShow = true
    },
    // 获取所有权限列表
    async assignList() {
      const res = await getPermissionList()
      this.assignForm = transListToTreeData(res, '0')
    },
    // 获取某员工的权限
    async oneAssognList() {
      const { permIds } = await getOneRole(this.roleId)
      this.permIdsList = permIds
    },
    // 权限业务结束

    // 角色业务开始
    // 添加角色
    async toAddRole() {
      this.showDialog = true
    },
    // 删除角色
    async toDelRole(id) {
      try {
        // 二次确认
        await this.$confirm('确认删除该角色吗?')
        await delRoles(id)
        // 重新加载数据
        await this.toGetRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑角色数据
    async toPutRoles(data) {
      this.roleForm = await getOneRole(data)
      // 显示弹框
      this.showDialog = true
    },
    // 确定
    async btnOK() {
      try {
        // 验证 rule
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          // 修改数据提交请求
          await putRoles(this.roleForm)
          // 刷新数据
          this.$message.success('编辑成功')
        } else {
          await addRoles(this.roleForm)
          this.$message.success('添加成功')
        }
        this.toGetRoleList()
        // 重置数据
        await this.$refs.roleForm.resetFields()
        this.showDialog = false
      } catch (error) {
        console.log('编辑/添加', error)
      }
    },
    // 取消
    async btnCancel() {
      // 复原所有的表单数据(包括有绑定和无绑定的,编辑时回显的数据)
      this.roleForm = {
        name: '',
        description: ''
      }
      // 数据重置
      await this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 获取公司信息
    async toGetCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 获取所有角色数据
    async toGetRoleList() {
      const res = await getRoleList(this.pages)
      this.list = res.rows
      this.total = res.total
    },
    // 角色业务结束

    // 更换页
    pageChange(newPage) {
      // 将这个用户点击的新页码赋值给咱们配置 this.page
      this.pages.page = newPage
      this.toGetRoleList()
    },
    // 更换每页数量
    sizeChange(newPagesize) {
      // 将这个用户点击的新页码赋值给咱们配置 this.page
      this.pages.pagesize = newPagesize
      this.toGetRoleList()
    }
  }
}
</script>

<style>
</style>
